<template>
  <div>
    <home-header></home-header>
    <home-swiper :list="swiperList"></home-swiper>
    <home-icons :list="iconList"></home-icons>
    <home-like :list="likeList"></home-like>
  </div>
</template>

<script>
import homeHeader from './components/Header'
import homeSwiper from './components/Swiper'
import homeIcons from './components/Icons'
import homeLike from './components/Like'
import axios from 'axios'
import { mapState } from 'vuex'

export default {
  name: 'Home',
  components: {
    homeHeader,
    homeSwiper,
    homeIcons,
    homeLike
  },
  data () {
    return {
      swiperList: [],
      iconList: [],
      lastCity: '',
      likeList: []
    }
  },
  methods: {
    getHomeInfo () {
      axios.get('/api/index.json', {
        params: {
          city: this.city
        }
      })
        .then(this.getHomeInfoSuccess)
    },
    getHomeInfoSuccess (res) {
      res = res.data
      if (res.ret && res.data) {
        let data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.likeList = data.likeList
      }
    }
  },
  computed: {
    ...mapState({
      city: 'city'
    })
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.getHomeInfo()
      this.lastCity = this.city
    }
  },
  mounted () { // 页面加载完成,发送ajax请求
    this.lastCity = this.city
    this.getHomeInfo()
  }
}
</script>

<style scoped>

</style>
